package com.yjd.bi.first.service.impl;

import com.alibaba.fastjson.JSON;
import com.yjd.bi.first.dao.IAppPayFirstDao;
import com.yjd.bi.first.model.AppPayFirstModel;
import com.yjd.bi.first.service.IAppPayFirstService;
import com.yjd.comm.base.dao.IBaseDao;
import com.yjd.comm.base.service.impl.BaseServiceImpl;
import com.yjd.comm.util.FrameUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <b>description</b>：首次充值记录业务实现<br>
 * <b>time</b>：2017-04-01 16:24:01 <br>
 * <b>author</b>：  carl
 */
@Service("appPayFirstService")
public class AppPayFirstServiceImpl extends BaseServiceImpl implements IAppPayFirstService {

    /**
     * 插入
     *
     * @param model
     * @return
     * @throws Exception
     */
    public AppPayFirstModel insert(AppPayFirstModel model) throws Exception {
        return this.appPayFirstDao.insertModel(model, true);
    }

    /**
     * 更新
     *
     * @param model
     * @return
     * @throws Exception
     */
    public int update(AppPayFirstModel model) throws Exception {
        return this.appPayFirstDao.updateModel(model, true);
    }

    /**
     * 根据id删除
     *
     * @param app_user_id
     * @return
     * @throws Exception
     */
    public int delete(long app_user_id) throws Exception {
        Map<Object, Object> paramMap = new HashMap<Object, Object>();
        paramMap.put("app_user_id", app_user_id);
        return this.appPayFirstDao.deleteModel(paramMap, true);
    }

    @Override
    public void generateAppPayFirst(Long startTime, Long endTime) throws Exception {
        if (startTime == null || endTime == null) {
            FrameUtil.throwBaseException1("请求参数异常");
        }

        Map param = new HashMap();
        param.put("starttime", startTime);
        param.put("endtime", endTime);

        List<AppPayFirstModel> firstPayList = this.appPayFirstDao.getAppFirstList(param);
        if(firstPayList==null||firstPayList.size()==0){
            logger.info("当前日期未查询到当天首次充值的用户，参数："+ JSON.toJSONString(param));
            return;
        }

        List<Long> userIds = new ArrayList<Long>();
        userIds.addAll(FrameUtil.getBeanFieldValues(firstPayList, "pay_user_id"));
        Map<Long,AppPayFirstModel>  existsMap =  this.appPayFirstDao.getModelMapByIds(userIds,false);

        if(existsMap!=null){
            Iterator<AppPayFirstModel> it = firstPayList.iterator();
            for(;it.hasNext();){
                AppPayFirstModel firstModel = it.next();
                if(existsMap.containsKey(firstModel.getApp_user_id())){
                    it.remove();
                }
            }
        }

        if(firstPayList.size()==0){
            logger.info("当前日期查询到当天首次充值且过滤后没有相关用户，参数："+ JSON.toJSONString(param));
            return;
        }

        try {
            this.appPayFirstDao.insertBatch(firstPayList, true);
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    @Override
    public Double getFirstPayUseTimes(Map param) throws Exception {
        return this.appPayFirstDao.getFirstPayUseTimes(param);
    }

    @Resource
    private IAppPayFirstDao appPayFirstDao;

    @Override
    public IBaseDao getBaseDao() {
        return this.appPayFirstDao;
    }

}
